<template>
  <div v-if="loading" style="text-align: center;padding: 50px;">
    <a-spin></a-spin>
  </div>
  <entity-view v-else @submit="submit" ref="signForm" :model="signInfo" :rules="rules"  :operator-type="operatorType">
    <div class="subtitle" v-if="this.pageInfo.mchntType !== '20'">商户证件信息</div>
    <div class="sub-content-flex" v-if="this.pageInfo.mchntType !== '20'">
      <form-item label="经营执照" v-model="signInfo.licenceImage" prop="licenceImage" v-if="!(pageInfo.mchntType === '20')">
        <a-upload
        list-type="picture-card"
        :disabled="this.operatorType!=='edit'&&this.operatorType!=='add'"
        :show-upload-list="false"
        :data="{'isup':1}"
        :before-upload="beforeUpload"
        :customRequest="licenceImgUpload"
      >
        <div v-if="this.licenceImageUrl !== ''">
          <img :src="this.licenceImageUrl" width="100px" height="100px">
          <a-tooltip placement="bottom">
            <template slot="title">
              <span>查看大图</span>
            </template>
            <a-icon :type="'eye'" theme="twoTone" twoToneColor="#4a9ff5" @click="viewLicenceImg"/>
          </a-tooltip>
          <a-tooltip placement="bottom" v-if="this.operatorType==='add'||this.operatorType==='edit'">
            <template slot="title">
              <span>清除图片</span>
            </template>
            <a-icon :type="'delete'" theme="twoTone" twoToneColor="#4a9ff5" @click="delLicenceImg" />
          </a-tooltip>
        </div>
        <div v-if="this.licenceImageUrl === ''">
          <a-icon :type="'plus'"/>
          <div class="ant-upload-text">上传</div>
        </div>
        </a-upload>
      </form-item>
      <form-item label="商户证件类型" input-type="select" prop="licenceType" v-model="signInfo.licenceType" :disabled="this.operatorType !== 'add'&&this.signInfo.auditStatus !== '00'&&this.signInfo.auditStatus !== '03'" :options='[
        {value: "00", name: "营业执照"},
        {value: "11", name: "贷款卡号"},
        {value: "99", name: "其他"},
        {value: "01", name: "机构信用代码证"},
        {value: "02", name: "民办非企业登记证书"},
        {value: "03", name: "全国组织机构代码证"},
        {value: "04", name: "事业单位法人证书"},
        {value: "05", name: "境外企业证书"},
        {value: "06", name: "社会团体法人登记证书"},
        {value: "07", name: "工会法人资格证书"},
        {value: "08", name: "政府批文"},
        {value: "09", name: "预先核准通知书"},
        {value: "10", name: "其他批文或证明"},
      ]' @change="licenceTypeChange"></form-item>
      <form-item label="商户证件号码" prop="licenceCode" v-model="signInfo.licenceCode" :disabled="this.operatorType !== 'add'&&this.signInfo.auditStatus !== '00'&&this.signInfo.auditStatus !== '03'"></form-item>

      <!-- TODO: 有未知判断条件 -->
      <form-item label="注册类型" input-type="select" prop="enterRegisterType" v-model="signInfo.enterRegisterType" :options='[
        {value: "000", name: "个体工商户"},
        {value: "010", name: "国有企业"},
        {value: "020", name: "集体企业"},
        {value: "030", name: "股份合作企业"},
        {value: "041", name: "国有联营企业"},
        {value: "042", name: "集体联营企业"},
        {value: "043", name: "国有与集体联营企业"},
        {value: "044", name: "其他联营企业"},
        {value: "051", name: "国有独资公司"},
        {value: "052", name: "其他有限责任公司"},
        {value: "060", name: "股份有限公司"},
        {value: "071", name: "私营独资企业"},
        {value: "072", name: "私营合伙企业"},
        {value: "073", name: "私营有限责任公司"},
        {value: "074", name: "私营股份有限公司"},
        {value: "080", name: "其他企业"},
        {value: "110", name: "合资经营企业"},
        {value: "120", name: "合作经营企业"},
        {value: "130", name: "独资经营企业"},
        {value: "140", name: "投资股份有限公司"},
        {value: "210", name: "中外合资经营企业"},
        {value: "220", name: "中外合作经营企业"},
        {value: "230", name: "外资企业"},
        {value: "240", name: "外商投资股份有限公司"},
        {value: "310", name: "政府机构"},
        {value: "320", name: "事业单位"},
      ]'></form-item>
      <!-- TODO: 有未知判断条件 -->
      <form-item label="注册资本（元）" v-if="pageInfo.registerCapitalFlag" prop="registerCapital" v-model="signInfo.registerCapital"></form-item>
      <form-item :label="signInfo.licenceCode === '00' ? '执照生效日期' : '商户证件生效日期'" prop="licenceEffectiveDate" date-format="YYYY-MM-DD" input-type="datepicker" v-model="signInfo.licenceEffectiveDate"></form-item>
      <form-item :label="signInfo.licenceCode === '00' ? '执照失效日期' : '商户证件失效日期'" prop="licenceExpiryDate">
        <a-date-picker v-model="signInfo.licenceExpiryDate" allow-clear date-format="YYYY-MM-DD"  :disabled="operatorType !== 'add' && operatorType !== 'edit'">
          <a-input v-if="signInfo.licenceExpiryDate === '长期'" allow-clear v-model="signInfo.licenceExpiryDate" :disabled="operatorType !== 'add' && operatorType !== 'edit'"></a-input>
        </a-date-picker>
        <a href="javascript:" @click="setLongTime('licenceExpiryDate')" v-if="operatorType === 'add' || operatorType === 'edit'" style="margin-left: 10px">长期</a>
      </form-item>
      <form-item label="注册地址" prop="registerAddress" v-model="signInfo.registerAddress" input-type="cascader" :options="address" :load-data="getAddress"></form-item>
      <form-item label="详细地址" prop="registerAddr" :disabled="operatorType !== 'add' && operatorType !== 'edit'" input-type="textarea" v-model="signInfo.registerAddr"></form-item>

      <form-item label="商户平均交易额(元)" v-if="pageInfo.mchntAvgTransAmt === 'Y'" prop="mchntAvgTransAmt" v-model="signInfo.mchntAvgTransAmt"></form-item>
    </div>
    <div class="subtitle">法人/负责人信息</div>
    <div class="sub-content-flex">
      <form-item label="身份证(人像面)" v-model="signInfo.certFaceImage" prop="certFaceImage">
        <a-upload list-type="picture-card"
                  :disabled="this.operatorType!=='edit'&&this.operatorType!=='add'"
                  :show-upload-list="false"
                  :data="{'isup':1}"
                  :before-upload="beforeUpload"
                  :customRequest="certFaceImgUpload"
        >
          <div v-if="this.certFaceImageUrl !== ''">
            <img :src="this.certFaceImageUrl" width="100px" height="100px">
            <a-tooltip placement="bottom">
              <template slot="title">
                <span>查看大图</span>
              </template>
              <a-icon :type="'eye'" theme="twoTone" twoToneColor="#4a9ff5" @click="viewCertFaceImg"/>
            </a-tooltip>
            <a-tooltip placement="bottom" v-if="this.operatorType==='add'||this.operatorType==='edit'">
              <template slot="title">
                <span>清除图片</span>
              </template>
              <a-icon :type="'delete'" theme="twoTone" twoToneColor="#4a9ff5" @click="delCertFaceImg" />
            </a-tooltip>
          </div>
          <div v-if="this.certFaceImageUrl === ''">
            <a-icon :type="'plus'"/>
            <div class="ant-upload-text">上传</div>
          </div>
        </a-upload>
      </form-item>
      <form-item label="身份证(国徽面)" v-model="signInfo.certBackImage" prop="certBackImage">
        <a-upload list-type="picture-card"
                  :disabled="this.operatorType!=='edit'&&this.operatorType!=='add'"
                  :show-upload-list="false"
                  :data="{'isup':1}"
                  :before-upload="beforeUpload"
                  :customRequest="certBackImgUpload"
        >
          <div v-if="this.certBackImageUrl !== ''">
            <img :src="this.certBackImageUrl" width="100px" height="100px">
            <a-tooltip placement="bottom">
              <template slot="title">
                <span>查看大图</span>
              </template>
              <a-icon :type="'eye'" theme="twoTone" twoToneColor="#4a9ff5" @click="viewCertBackImg"/>
            </a-tooltip>
            <a-tooltip placement="bottom" v-if="this.operatorType==='add'||this.operatorType==='edit'">
              <template slot="title">
                <span>清除图片</span>
              </template>
              <a-icon :type="'delete'" theme="twoTone" twoToneColor="#4a9ff5" @click="delCertBackImg" />
            </a-tooltip>
          </div>
          <div v-if="this.certBackImageUrl === ''">
            <a-icon :type="'plus'"/>
            <div class="ant-upload-text">上传</div>
          </div>
        </a-upload>
      </form-item>
      <form-item label="法人姓名" prop="artifName" v-model="signInfo.artifName" :valid="true" :disabled="this.operatorType !== 'add'&&this.signInfo.auditStatus !== '00'&&this.signInfo.auditStatus !== '03'"></form-item>
      <form-item label="法人证件类型" prop="artifCertType" v-model="signInfo.artifCertType" @change="artifCertTypeChange":valid="true" :disabled="this.operatorType !== 'add'&&this.signInfo.auditStatus !== '00'&&this.signInfo.auditStatus !== '03'" input-type="select" :options="[
        {value: '99', name: '其它'},
        {value: '01', name: '身份证'},
        {value: '02', name: '军官证'},
        {value: '03', name: '护照'},
        {value: '04', name: '户口薄'},
        {value: '05', name: '士兵证'},
        {value: '06', name: '港澳来往内地通行证'},
        {value: '07', name: '台湾同胞来往内地通行证'},
        {value: '08', name: '临时身份证'},
        {value: '09', name: '外国人居留证'},
      ]"></form-item>
      <form-item label="法人证件号码" prop="artifCertNo" v-model="signInfo.artifCertNo" @click.native="setArtifCertNo" :valid="true" :disabled="this.operatorType !== 'add'&&this.signInfo.auditStatus !== '00'&&this.signInfo.auditStatus !== '03'"></form-item>
      <form-item label="证件生效日期" prop="certEffectiveDate" v-model="signInfo.certEffectiveDate" input-type="datepicker" date-format="YYYY-MM-DD"></form-item>
      <form-item label="证件失效日期"  v-model="signInfo.certExpiryDate" prop="certExpiryDate">
        <a-date-picker  allow-clear  :disabled="operatorType !== 'add' && operatorType !== 'edit'"  date-format="YYYY-MM-DD">
          <a-input v-if="signInfo.certExpiryDate === '长期'" allow-clear v-model="signInfo.certExpiryDate" :disabled="operatorType !== 'add' && operatorType !== 'edit'"></a-input>
        </a-date-picker>
        <a href="javascript:" @click="setLongTime('certExpiryDate')" v-if="operatorType === 'add' || operatorType === 'edit'" style="margin-left: 10px">长期</a>
      </form-item>
      <form-item label="法人手机号" prop="artifMobile" v-model="signInfo.artifMobile"></form-item>
      <form-item label="法人电子邮箱" prop="artifEmail" v-model="signInfo.artifEmail"></form-item>

    </div>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="this.imageUrl" />
    </a-modal>
    <div class="btn-wrapper">
      <a-button type="primary" @click="previous">上一步</a-button>
      <a-button type="primary" @click="onlineVerification" v-if="pageInfo.onLineCheckFlag === 'Y' && (operatorType === 'add' || operatorType === 'edit')">联网核查</a-button>
      <a-button type="primary" @click="creditReport" v-if="pageInfo.creditReportFlag === 'Y' && (operatorType === 'add' || operatorType === 'edit')">征信查询</a-button>
      <a-button type="primary" html-type="submit">下一步</a-button>
    </div>
    <a-modal v-model="visible" :title="modelTitle" ok-text="关闭" @ok="hideModal">
      <p>证件号码：{{this.signInfo.artifCertNo}}</p>
      <p>{{this.checkResult}}</p>
    </a-modal>
  </entity-view>
</template>

<script>
import EntityView from '../../../../components/common/entityView/EntityView'
import FormItem from '../../../../components/common/entityView/FormItem'
import moment from 'moment';
export default {
  name: 'SignInfoForm',
  components: {EntityView, FormItem},
  inject: ['merchantBaseInfoContext', 'operationContext'],
  data: function () {
    return {
      pageInfo: {
        mchntType: ''
      },
      signInfo: {
        registerAddress:[],
        licenceImage:{},
        certFaceImage:{},
        certBackImage:{},
        artifCertType:"",
      },
      address: [],
      rules: {
        licenceImage: [{required: true, message: '请上传经营执照', trigger: 'blur'}],
        licenceCode: [{required: true, message: '请输入商户证件号码', trigger: 'blur'}],
        enterRegisterType: [{required: true, message: '请选择注册类型', trigger: 'blur'}],
        licenceEffectiveDate: [{required: true, message: '请选择生效日期', trigger: 'blur'}],
        licenceExpiryDate: [{required: true, message: '请选择失效时期', trigger: 'blur'}],
        registerAddress: [{required: true, message: '请选择注册地址', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (value.length === 3 ? undefined : new Error('请输入正确的格式')) : new Error('请选择营业地址'))
          },
          trigger: 'blur'
        }],
        registerCapital: [{required: false, message: '请输入注册资本（元）', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/^[1-9][0-9]*$/.test(value) ? undefined : new Error('请输入正确的格式')) : undefined)
          },
          trigger: 'blur'
        }],
        registerAddr: [{required: true, message: '请输入详细注册地址', trigger: 'blur'}],
        certFaceImage: [{required: true, message: '请上传身份证(人像面)', trigger: 'blur'}],
        certBackImage: [{required: true, message: '请上传身份证(国徽面)', trigger: 'blur'}],
        artifName: [{required: true, message: '请输入法人姓名', trigger: 'blur'}],
        artifCertType: [{required: true, message: '请选择法人证件类型', trigger: 'blur'}],
        artifCertNo: [{required: true, message: '请输入法人证件号码', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/.test(value) ? undefined : new Error('请输入正确的身份证格式')) : undefined)
          },
          trigger: 'blur'
        }],
        artifEmail: [{required: false, message: '请输入法人电子邮箱', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value) ? undefined : new Error('请输入正确的邮箱格式')) : undefined)
          },
          trigger: 'blur'
        }],
        certEffectiveDate: [{required: true, message: '请选择法人证件生效日期', trigger: 'change'}],
        certExpiryDate: [{required: true, message: '请选择法人证件失效日期', trigger: 'change'}],
        artifMobile: [{required: true, message: '请输入法人手机号', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/^1[0-9]{10}$/.test(value) ? undefined : new Error('请输入正确的格式')) : undefined)
          },
          trigger: 'blur'
        }],
      },
      operatorType:"add",
      licenceImageUrl:"",
      certFaceImageUrl:"",
      certBackImageUrl:"",
      licenceImageId:"",
      certFaceImageId:"",
      certBackImageId:"",
      previewVisible:false,
      imageUrl:"",
      checkResult:"",
      modelTitle:"",
      visible:false,
      loading:false,
    }
  },
  methods: {
    async getPageInfo () {
      try {
        this.resetFields();
        this.operatorType = this.operationContext.operatorType;
        let optFlag = {add: `add`, edit: `upd`, view: `view`, use: `use`, stop: `stop`, cancel: `cancel`,chk: `chk`}[this.operatorType]
        this.loading = true;
        const param = {
          optFlag: optFlag,
          instId: this.merchantBaseInfoContext.query.instId,
          mchntNo: this.operationContext.beanInfo.mchntNo
        }
        this.loading = true
        const res = await this.$get('/admin/mcmp/bMchntBaseInfo/editSignInfo',param);
        this.loading = false;
        this.licenceImageUrl = ''
        this.certFaceImageUrl = ''
        this.certBackImageUrl = ''
        this.signInfo = res.data.entity
        this.licenceTypeChange(this.signInfo.licenceType)
        this.signInfo.mchntNo = this.operationContext.beanInfo.mchntNo
        this.pageInfo = res.data
        if(optFlag !== 'add') {
          this.artifCertTypeChange(this.signInfo.artifCertType)
          this.signInfo.registerAddress = []
          this.signInfo.registerAddress.push(this.signInfo.registerProvince)
          this.signInfo.registerAddress.push(this.signInfo.registerCity)
          this.signInfo.registerAddress.push(this.signInfo.registerArea)
          if (this.signInfo.licenceEffectiveDate) {
            this.signInfo.licenceEffectiveDate = moment(this.signInfo.licenceEffectiveDate,'YYYY-MM-DD')
          }
          if (this.signInfo.licenceExpiryDate && this.signInfo.licenceExpiryDate !== '长期') {
            this.signInfo.licenceExpiryDate = moment(this.signInfo.licenceExpiryDate,'YYYY-MM-DD')
          }
          if (this.signInfo.certEffectiveDate) {
            this.signInfo.certEffectiveDate = moment(this.signInfo.certEffectiveDate,'YYYY-MM-DD')
          }
          if (this.signInfo.certExpiryDate && this.signInfo.certExpiryDate !== '长期') {
            this.signInfo.certExpiryDate = moment(this.signInfo.certExpiryDate,'YYYY-MM-DD')
          }
          if(typeof this.signInfo.licenceImage.id !== 'undefined'){
            this.licenceImageId = this.signInfo.licenceImage.id
            await this.getLicenceImg();
          }
          if(typeof this.signInfo.certFaceImage.id !== 'undefined'){
            this.certFaceImageId = this.signInfo.certFaceImage.id
            await this.getCertFaceImg();
          }
          if(typeof this.signInfo.certBackImage.id !== 'undefined'){
            this.certBackImageId = this.signInfo.certBackImage.id
            await this.getCertBackImg();
          }
        }
      } catch (e) {
        console.error(e)
        this.loading = false;
      }
    },
    async submit () {
      try {
        if(this.operatorType === 'edit' || this.operatorType === 'add'){
          const res = await this.$refs.signForm.validate()
          if (res) {
            const params = Object.assign({}, this.signInfo)
            if(this.pageInfo.mchntType === '20'){
              params.enterRegisterType='000';
              params.licenceType='';
            }
            if(params.auditStatus === '00'){
              params.optFlag = 'add'
            }
            if (params.registerAddress) {
              params.registerProvince = params.registerAddress[0]
              params.registerCity = params.registerAddress[1]
              params.registerRegion = params.registerAddress[2]
            }
            if (params.licenceEffectiveDate) {
              params.licenceEffectiveDate = moment(params.licenceEffectiveDate).format('YYYYMMDD')
            }
            if (params.licenceExpiryDate && params.licenceExpiryDate !== '长期') {
              params.licenceExpiryDate = moment(params.licenceExpiryDate).format('YYYYMMDD')
            }
            if (params.certEffectiveDate) {
              params.certEffectiveDate = moment(params.certEffectiveDate).format('YYYYMMDD')
            }
            if (params.certExpiryDate && params.certExpiryDate !== '长期') {
              params.certExpiryDate = moment(params.certExpiryDate).format('YYYYMMDD')
            }
            const res = await this.$post('/admin/mcmp/bMchntBaseInfo/editSignInfo', params)
            if (res.data.respCode === "00000000") {
              this.$message.success('保存成功！')
              this.$emit("submit");
              return true
            } else {
              this.$message.error(res.data.respMsg)
              return false
            }
          }
        } else {
          this.$emit('submit', ...arguments)
        }
      } catch (e) {
        console.error(e)
      }
      return false
    },
    previous () {
      this.operationContext.tabActiveKey = 'baseInfo';
    },
    resetFields () {
      return this.$refs.signForm.resetFields()
    },
    async onlineVerification () {
      if(this.signInfo.artifCertType !== '01'){
        this.warning('法人证件类型非身份证不需要联网核查！')
        return false;
      }
      const params = {
        instId:this.merchantBaseInfoContext.query.instId,
        artifName:this.signInfo.artifName,
        certNo:this.signInfo.artifCertNo,
        mchntNo:this.operationContext.beanInfo.mchntNo,
        certType:'01',
        certEffectiveDate:this.signInfo.certEffectiveDate,
        certExpiryDate:this.signInfo.certExpiryDate,
      }
      if (params.certEffectiveDate) {
        params.certEffectiveDate = params.certEffectiveDate.format('YYYY-MM-DD')
      }
      if (params.certExpiryDate && params.certExpiryDate !== '长期') {
        params.certExpiryDate = params.certExpiryDate.format('YYYY-MM-DD')
      }
      const res = await this.$post('/admin/mcmp/bMchntBaseInfo/onlineVerification', params)
      if (res.data.code === "100000") {
        this.visible = true
        this.modelTitle = "联网核查详情"
        this.checkResult = '核查结果：公民身份号码与姓名一致，且存在照片';
        return true
      } else {
        this.$message.error(res.data.message)
        return false
      }
    },
    async creditReport () {
      const params = {
        instId:this.merchantBaseInfoContext.query.instId,
        artifName:this.signInfo.artifName,
        certNo:this.signInfo.artifCertNo,
        mchntNo:this.operationContext.beanInfo.mchntNo,
        certType:this.signInfo.artifCertType,
        certEffectiveDate:this.signInfo.certEffectiveDate,
        certExpiryDate:this.signInfo.certExpiryDate,
      }
      if (params.certEffectiveDate) {
        params.certEffectiveDate = params.certEffectiveDate.format('YYYY-MM-DD')
      }
      if (params.certExpiryDate && params.certExpiryDate !== '长期') {
        params.certExpiryDate = params.certExpiryDate.format('YYYY-MM-DD')
      }
      const res = await this.$post('/admin/mcmp/bMchntBaseInfo/creditReport', params)
      if (res.data.code === "100000") {
        this.visible = true
        this.modelTitle = "征信查询详情"
        this.checkResult = '征信结果：' + res.data.creditReportContent;
        return true
      } else {
        this.$message.error(res.data.message)
        return false
      }
    },
    hideModal(){
      this.visible = false;
    },
    licenceTypeChange (val) {
      if (val === '00') {
        this.$set(this.rules, 'licenceImage', [{required: true, message: '请上传经营执照', trigger: 'blur'}])
        this.$set(this.rules, 'licenceEffectiveDate', [{required: true, message: '请选择生效日期', trigger: 'blur'}])
        this.$set(this.rules, 'licenceExpiryDate', [{required: true, message: '请选择失效时期', trigger: 'blur'}])
      } else {
        this.$set(this.rules, 'licenceImage', [])
        this.$set(this.rules, 'licenceEffectiveDate', [])
        this.$set(this.rules, 'licenceExpiryDate', [])
      }
      if(val === '99'){
        this.$set(this.rules, 'licenceCode', [])
      }else{
        this.$set(this.rules, 'licenceCode', [{required: true, message: '请输入商户证件号码', trigger: 'blur'}])
      }
    },
    artifCertTypeChange (val) {
      this.signInfo.artifCertType = val;
      if (val === '01') {
        this.$set(this.rules, 'artifCertNo', [{required: true, message: '请输入法人证件号码', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/.test(value) ? undefined : new Error('请输入正确的身份证格式')) : undefined)
          },
          trigger: 'blur'
        }])
      } else {
        this.$set(this.rules, 'artifCertNo', [{required: true, message: '请输入法人证件号码', trigger: 'blur'}, {
          validator(rule, value, callback) {
            callback(value ? (/^[a-zA-Z1-9][a-zA-Z0-9]{0,30}$/.test(value) ? undefined : new Error('请输入正确的证件格式')) : undefined)
          },
          trigger: 'blur'
        }])
      }
    },
    setArtifCertNo(){
      if(typeof this.signInfo.artifCertType === 'undefined' || this.signInfo.artifCertType === ''){
        this.warning('请先选择法人证件类型')
      }
    },
    async getAddress (selectedOptions) {
      let targetOption
      if (selectedOptions) {
        targetOption = selectedOptions[selectedOptions.length - 1];
        targetOption.loading = true;
      }

      const res = await this.$get('/admin/mcmp/bMchntBaseInfo/registerselect2', {
        q: encodeURIComponent(`&${this.merchantBaseInfoContext.query.instId}&${(targetOption && targetOption.nextLevel || '01')}&${targetOption ? targetOption.id : ''}`),
        pageSize: 100,
        pageNum: 1,
      })

      targetOption && (targetOption.loading = false)
      const address = ((res.data && res.data.results) || []).map(i => {
        i.label = i.name;
        i.value = i.id;
        i.isLeaf = targetOption ? (targetOption.nextLevel === '03') : false // 是否是最后的节点
        i.nextLevel = targetOption ? '03' : '02'
        return i
      })
      if (targetOption) {
        targetOption.children = address
        this.address = [...this.address]
      } else {
        this.address = address
      }

    },

    setLongTime(field){
      this.$set(this.signInfo, field, '长期')
    },
    handleCancel() {
      this.previewVisible = false;
    },
    viewLicenceImg(e) {
      this.imageUrl = this.licenceImageUrl;
      this.previewVisible = true;
      e.stopPropagation();
    },
    viewCertFaceImg(e) {
      this.imageUrl = this.certFaceImageUrl;
      this.previewVisible = true;
      e.stopPropagation();
    },
    viewCertBackImg(e) {
      this.imageUrl = this.certBackImageUrl;
      this.previewVisible = true;
      e.stopPropagation();
    },
    delLicenceImg(e) {
      this.licenceImageUrl = "";
      this.$set(this.signInfo,'licenceImage',null)
      e.stopPropagation();
    },
    delCertFaceImg(e) {
      this.certFaceImageUrl = "";
      this.$set(this.signInfo,'certFaceImage',null)
      e.stopPropagation();
    },
    delCertBackImg(e) {
      this.certBackImageUrl = "";
      this.$set(this.signInfo,'certBackImage',null)
      e.stopPropagation();
    },
    async licenceImgUpload({action, file, onSuccess, onError, onProgress}) {
      const formData = new FormData();
      formData.append('imgFile', file);
      formData.append('imageType', "00");
      formData.append('instId', this.merchantBaseInfoContext.query.instId);
      formData.append('mchntNo', this.operationContext.beanInfo.mchntNo);
      const res = await this.$upload(
        "/admin/mcmp/bMchntImageInfo/uploadBomp",formData
      );
      if (res.data.code === "100000") {
        this.signInfo.licenceImage = {
          id:this.licenceImageId,
          imageLocalPath:res.data.data.imageLocalPath,
          imageIndex:"0",
          imageType:"00"
        }
        this.$set(this.signInfo,'licenceImage',this.signInfo.licenceImage)
        this.licenceImageUrl = res.data.data.imageData;
        this.$message.success("上传成功！");
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },
    async certFaceImgUpload({action, file, onSuccess, onError, onProgress}) {
      const formData = new FormData();
      formData.append('imgFile', file);
      formData.append('imageType', "00");
      formData.append('instId', this.merchantBaseInfoContext.query.instId);
      formData.append('mchntNo', this.operationContext.beanInfo.mchntNo);
      const res = await this.$upload(
        "/admin/mcmp/bMchntImageInfo/uploadBomp",formData
      );
      if (res.data.code === "100000") {
        this.signInfo.certFaceImage = {
          id:this.certFaceImageId,
          imageLocalPath:res.data.data.imageLocalPath,
          imageIndex:"0",
          imageType:"01"
        }
        this.$set(this.signInfo,'certFaceImage',this.signInfo.certFaceImage)
        this.certFaceImageUrl = res.data.data.imageData;
        this.$message.success("上传成功！");
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },
    async certBackImgUpload({action, file, onSuccess, onError, onProgress}) {
      const formData = new FormData();
      formData.append('imgFile', file);
      formData.append('imageType', "00");
      formData.append('instId', this.merchantBaseInfoContext.query.instId);
      formData.append('mchntNo', this.operationContext.beanInfo.mchntNo);
      const res = await this.$upload(
        "/admin/mcmp/bMchntImageInfo/uploadBomp",formData
      );
      if (res.data.code === "100000") {

        this.signInfo.certBackImage = {
          id:this.certBackImageId,
          imageLocalPath:res.data.data.imageLocalPath,
          imageIndex:"0",
          imageType:"02"
        }
        this.$set(this.signInfo,'certBackImage',this.signInfo.certBackImage)
        this.certBackImageUrl = res.data.data.imageData;
        this.$message.success("上传成功！");
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },

    async getLicenceImg(){
      const params = {
        imageLocalPath: this.signInfo.licenceImage.imageLocalPath,
        imgId: this.signInfo.licenceImage.id,
        imageType: "00",
        instId: this.merchantBaseInfoContext.query.instId,
      };
      const res = await this.$get(
        "/admin/mcmp/bMchntImageInfo/getBompImg",params
      );
      if (res.data.code === "100000") {
        this.licenceImageUrl = res.data.data.imageData;
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },
    async getCertFaceImg(){
      const params = {
        imageLocalPath: this.signInfo.certFaceImage.imageLocalPath,
        imgId: this.signInfo.certFaceImage.id,
        imageType: "00",
        instId: this.merchantBaseInfoContext.query.instId,
      };
      const res = await this.$get(
        "/admin/mcmp/bMchntImageInfo/getBompImg",params
      );
      if (res.data.code === "100000") {
        this.certFaceImageUrl = res.data.data.imageData;
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },
    async getCertBackImg(){
      const params = {
        imageLocalPath: this.signInfo.certBackImage.imageLocalPath,
        imgId:  this.signInfo.certBackImage.id,
        imageType: "00",
        instId: this.merchantBaseInfoContext.query.instId,
      };
      const res = await this.$get(
        "/admin/mcmp/bMchntImageInfo/getBompImg",params
      );
      if (res.data.code === "100000") {
        this.certBackImageUrl = res.data.data.imageData;
        return true;
      } else {
        this.$message.error(res.data.data.respDesc);
        return false;
      }
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/bmp'
      if (!isJPG) {
        this.$message.error('请上传图片文件');
      }
      return isJPG;
    },
    warning(message){
      const h = this.$createElement;
      this.$warning({
        title: '提示',
        content: h('div', {}, [
          h('p', message),
        ]),
        onOk() {},
      });
    }
  },
  mounted () {
    this.getPageInfo()
    this.getAddress()
  }
}
</script>

<style scoped>
.btn-wrapper{
  padding: 0 10px;
  width: 100%;
  text-align: center;
}
.subtitle {
  width: 100%;
  font-size: 16px;
  margin-bottom: 10px;
  padding: 0 10px;
}

.sub-content-flex {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}

.sub-content-block {
  display: block;
  flex-wrap: wrap;
  width: 100%;
  padding: 0 10px;
}
</style>
